﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _24点
{
    public class Solution
    {
        public int[] number = new int[4];
        public static readonly char[] ops = { '+', '-', '*', '/' };

        public Solution(int[] number)
        {
            this.number = number;
        }

        public List<int[]> NumberList()
        {
            List<int[]> list = new();
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        for (int l = 0; l < 4; l++)
                        {
                            if (i == j || i == k || i == l || j == k || j == l || k == l)
                            {
                                continue;
                            }
                            list.Add(new int[] { number[i], number[j], number[k], number[l] });
                        }
                    }
                }
            }
            return list;
        }

        public static float Result(float num1, float num2, int op)
        {
            switch (op)
            {
                //+
                case 0:
                    return num1 + num2;
                //-
                case 1:
                    return num1 - num2;
                //*
                case 2:
                    return num1 * num2;
                ///
                default:
                    if (Math.Abs(num2) < 1e-4)
                    {
                        throw new Exception("除零错误");
                    }
                    return num1 * 1f / num2;
            }
        }

        /// <summary>
        /// ((a,b)c)d
        /// </summary>
        /// <param name="nums"></param>
        public void Fun1(int[] nums, HashSet<string> set)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        try
                        {
                            float sum = Result(nums[0], nums[1], i);
                            sum = Result(sum, nums[2], j);
                            sum = Result(sum, nums[3], k);

                            if (Math.Abs(sum - 24) < 1e-4)
                            {
                                set.Add($"(({nums[0]}{ops[i]}{nums[1]}){ops[j]}{nums[2]}){ops[k]}{nums[3]})");
                            }
                        }
                        catch { }
                    }
                }
            }
        }

        /// <summary>
        /// (a,b)(c,d)
        /// </summary>
        /// <param name="nums"></param>
        public void Fun2(int[] nums, HashSet<string> set)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        try
                        {
                            float sum1 = Result(nums[0], nums[1], i);
                            float sum2 = Result(nums[2], nums[3], k);
                            float sum = Result(sum1, sum2, j);

                            if (Math.Abs(sum - 24) < 1e-4)
                            {
                                set.Add($"({nums[0]}{ops[i]}{nums[1]}){ops[j]}({nums[2]}{ops[k]}{nums[3]})");
                            }
                        }
                        catch { }
                    }
                }
            }
        }

        /// <summary>
        /// (a(b,c))d
        /// </summary>
        /// <param name="nums"></param>
        public void Fun3(int[] nums, HashSet<string> set)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        try
                        {
                            float sum = Result(nums[1], nums[2], j);
                            sum = Result(nums[0], sum, i);
                            sum = Result(sum, nums[3], k);

                            if (Math.Abs(sum - 24) < 1e-4)
                            {
                                set.Add( $"({nums[0]}{ops[i]}({nums[1]}{ops[j]}{nums[2]})){ops[k]}{nums[3]}");
                            }
                        }
                        catch { }
                    }
                }
            }
        }

        /// <summary>
        /// a((b,c)d)
        /// </summary>
        /// <param name="nums"></param>
        public void Fun4(int[] nums, HashSet<string> set)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        try
                        {
                            float sum = Result(nums[1], nums[2], j);
                            sum = Result(sum, nums[3], k);
                            sum = Result(nums[0], sum, i);

                            if (Math.Abs(sum - 24) < 1e-4)
                            {
                                set.Add( $"{nums[0]}{ops[i]}(({nums[1]}{ops[j]}{nums[2]}){ops[k]}{nums[3]})");
                            }
                        }
                        catch { }
                    }
                }
            }
        }

        /// <summary>
        /// a(b(c,d))
        /// </summary>
        /// <param name="nums"></param>
        public void Fun5(int[] nums, HashSet<string> set)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        try
                        {
                            float sum = Result(nums[2], nums[3], k);
                            sum = Result(nums[1], sum, j);
                            sum = Result(nums[0], sum, i);

                            if (Math.Abs(sum - 24) < 1e-4)
                            {
                                set.Add( $"{nums[0]}{ops[i]}({nums[1]}{ops[j]}({nums[2]}{ops[k]}{nums[3]}))");
                            }
                        }
                        catch { }
                    }
                }
            }
        }
    }
}
